https://github.com/ted59438/EnglishVocabulary_MySQL
/// <summary>
/// 編輯學生按紐事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editStudentBtn_Click(object sender, EventArgs e)
{
if (queryResultGrid.CurrentRow == null)
{
return;
}
// 判斷所有欄位是否已經輸入
TextBox[] fields = { studentNameTextBox, studentUsernameTextBox};
string errorMsg = fieldsIsEmpty(fields);
if (!string.IsNullOrEmpty(errorMsg))
{
MessageBox.Show(errorMsg.ToString(), "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 判斷帳號是否已經存在
if (usernameIsExists(studentUsernameTextBox.Text))
{
MessageBox.Show("帳號已經存在!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 修改學生資料
Student student = getStudentFromField();
string sql = @"UPDATE Student
SET RealName = @RealName,
Birthdate = @Birthdate,
Username = @Username
WHERE StudentID = @StudentID";
Dictionary<string, object> parameters = new Dictionary<string, object>()
{
{"RealName", student.RealName },
{"Birthdate", student.Birthdate },
{"Username", student.Username },
{"StudentID", student.StudentID}
};
try
{
queryNoneReturn(sql, parameters);
queryStudentBtn.PerformClick();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 刪除學生按紐事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteStudentBtn_Click(object sender, EventArgs e)
{
if (queryResultGrid.CurrentRow == null)
{
return;
}
Student student = getStudentFromField();
string sql = @"DELETE
FROM Student
WHERE StudentID = @StudentID";
Dictionary<string, object> parameters = new Dictionary<string, object>()
{
{"StudentID", student.StudentID }
};
try
{
queryNoneReturn(sql, parameters);
queryStudentBtn.PerformClick();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 取得所有學生資料的按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void queryStudentBtn_Click(object sender, EventArgs e)
{
try
{
string sql = @"SELECT StudentID,
RealName,
Username,
CAST(Birthdate AS DATE) AS Birthdate
FROM Student";
queryResultGrid.DataSource = queryDT(sql, new Dictionary<string, object>());
queryResultGrid.Columns["RealName"].HeaderText = "姓名";
queryResultGrid.Columns["Username"].HeaderText = "帳號";
queryResultGrid.Columns["Birthdate"].HeaderText = "生日";
queryResultGrid.Columns["StudentID"].Visible = false;
queryResultGrid.ClearSelection();
studentNameTextBox.Text = studentUsernameTextBox.Text = studentPasswordTextBox.Text = "";
insertStudentBtn.Enabled = editStudentBtn.Enabled = deleteStudentBtn.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public class Student
{
/// <summary>
/// 學生資料ID
/// </summary>
public string StudentID;
/// <summary>
/// 姓名
/// </summary>
public string RealName;
/// <summary>
/// 生日
/// </summary>
public DateTime Birthdate;
/// <summary>
/// 帳號
/// </summary>
public string Username;
/// <summary>
/// 密碼
/// </summary>
public string Password;
}
以英文單字測驗為例,共有四個實體
將學生資料以物件的表達方式,我們稱為「實體(Entity)」
An entity is any object in the system that we want to model and store information about. Entities are usually recognizable concepts, either concrete or abstract, such as person, places, things, or events which have relevance to the database.
到目前為止,我們實作的所有學生管理的功能都是以Raw SQL (原始SQL) 操作資料庫
除了使用Raw SQL處理資料,另一個以資料實體處理資料的寫法,稱之為ORM。